package top.baselitch.sample;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import top.baselitch.liteorm.DaoExecutor;
import top.baselitch.liteorm.LiteDaoFactory;
import top.baselitch.sample.adapter.TableAdapter;
import top.baselitch.sample.beans.User;
import top.baselitch.sample.dao.UsersDao;
import top.baselitch.sample.dbutils.DaoFactory;
import top.baselitch.sample.dbutils.UserDao;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemLongClickListener{
    private ListView lv;
    private Button addBtn,delBtn,changeBtn,reflushBtb;
    private TableAdapter mAdapter;
    private List<User> users;
    private UserDao mDao;
    private UsersDao usersDao;
    private int nowIndex;//当前选中项目
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table);
        initView();
//        initListView();
        initViewEvent();
        usersDao = LiteDaoFactory.create(UsersDao.class);
        DaoExecutor<Void> executor = usersDao.getAllUser();
       executor.exec(Void.class);
//        mDao = DaoFactory.getUserDao(this);
//        getUsersFromDatabase();
    }
    private void initView(){
        lv = (ListView) findViewById(R.id.table_lv);
        addBtn = (Button) findViewById(R.id.table_add);
        delBtn = (Button) findViewById(R.id.table_del);
        changeBtn = (Button) findViewById(R.id.table_change);
        reflushBtb = (Button) findViewById(R.id.table_reflush);
        delBtn.setEnabled(false);
        changeBtn.setEnabled(false);
    }
    private void initViewEvent(){
        addBtn.setOnClickListener(this);
        delBtn.setOnClickListener(this);
        changeBtn.setOnClickListener(this);
        reflushBtb.setOnClickListener(this);
        lv.setOnItemLongClickListener(this);
    }
    private void initListView(){
        users = new ArrayList<>();
        mAdapter = new TableAdapter(this,users);
        lv.setAdapter(mAdapter);
    }

    private void getSampleData(){
        for(int i=0;i<10;i++){
            User user = new User();
            user.setUserName("Litch");
            user.setEmail("therichard@foxmail.com");
            user.setPasswd("anygod.com");
            users.add(user);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,UserInfoActivity.class);
        Bundle bundle = new Bundle();
        switch (v.getId()){
            case R.id.table_add:
                Log.e("aaa", "0000");
                bundle.putString(UserInfoActivity.INTENT.TITLE,"添加用户");
                bundle.putInt(UserInfoActivity.INTENT.TAG,UserInfoActivity.TAG_ADD);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.table_del:
                delUser();
                break;
            case R.id.table_change:
                bundle.putString(UserInfoActivity.INTENT.TITLE,"修改用户信息");
                bundle.putInt(UserInfoActivity.INTENT.TAG,UserInfoActivity.TAG_CHANGE);
                bundle.putSerializable(UserInfoActivity.INTENT.USER,users.get(nowIndex));
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.table_reflush:
                getUsersFromDatabase();
                changeBtnStatus();
                break;
        }
    }
    private void getUsersFromDatabase(){
        users.clear();
        users.addAll(mDao.getUsers());
        mAdapter.notifyDataSetChanged();
        mAdapter.clearFlg();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        mAdapter.setFlg(position,!mAdapter.getFlg(position));
        nowIndex = position;
        changeBtnStatus();
        return true;
    }

    private void changeBtnStatus(){
        if(mAdapter.isAllFlg()){
            delBtn.setEnabled(true);
            changeBtn.setEnabled(true);
        }else{
            delBtn.setEnabled(false);
            changeBtn.setEnabled(false);
        }
    }

    private void delUser(){
        mDao.delUser(users.get(nowIndex).getUserId());
        getUsersFromDatabase();
        changeBtnStatus();
    }
}
